package be.idamf.mx.rpc
{
	import flash.events.EventDispatcher;
	
	import mx.messaging.messages.AsyncMessage;
	import mx.rpc.AsyncToken;
	import mx.rpc.Fault;
	import mx.rpc.IResponder;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.InvokeEvent;
	import mx.rpc.events.ResultEvent;

	[Event(name="invoke", type="mx.rpc.events.InvokeEvent")]
	[Event(name="result", type="mx.rpc.events.ResultEvent")]
	[Event(name="fault", type="mx.rpc.events.FaultEvent")]
	
	public class MockService extends EventDispatcher implements IRPCService
	{
		[Bindable]
		public var destination:String;
		
		protected var responder:IResponder;
		
		////////////////////////////////////////////////////////////////////////
		
		/**
		 * Constructor
		 */
		public function MockService(destination:String = null, target:IEventDispatcher = null)
		{
			super(target);
			this.destination = destination;
			responder = new Responder(result, fault);
		}
		
		////////////////////////////////////////////////////////////////////////
		
		/**
		 * Triggers an async result.
		 */
		public function asyncResult(result:Object, operation:String = null):AsyncToken
		{
			var message:MockMessage = new MockMessage();
			message.destination = destination;
			message.operation = operation || "";
			var token:MockAsyncToken = new MockAsyncToken(message);
			token.asyncResult(result);
			token.addResponder(responder);
			dispatchEvent(InvokeEvent.createEvent(token, message));
			return token;
		}

		/**
		 * Triggers an async fault.
		 */
		public function asyncFault(fault:Fault, operation:String = null):AsyncToken
		{
			var message:MockMessage = new MockMessage();
			message.destination = destination;
			message.operation = operation || "";
			var token:MockAsyncToken = new MockAsyncToken(message);
			token.asyncFault(fault);
			token.addResponder(responder);
			dispatchEvent(InvokeEvent.createEvent(token, message));
			return token;
		}
		
		////////////////////////////////////////////////////////////////////////
		
		/**
		 * @private
		 */
		protected function result(data:Object):void
		{
			dispatchEvent(ResultEvent(data));
		}

		/**
		 * @private
		 */
		protected function fault(info:Object):void
		{
			dispatchEvent(FaultEvent(info));
		}
	}
}

import mx.messaging.messages.AsyncMessage;

internal class MockMessage extends AsyncMessage
{
	public var operation:String;
	
	public function MockMessage(body:Object = null, headers:Object = null)
	{
		super(body, headers);
	}
}